import store from "../commons/store"
import Request from "../commons/Request";
import { mapGetters } from "vuex";

export const commonMixin = {
    data(){
        return {
            Perm:{},
        }
    },
    props: {
        isSelect: {
            type: Boolean,
            default: true
        }
    },
    watch: {
        isSelect(newVal,oldVal) {
            if(newVal){
                this.initView(false);
            }
        }
    },
    computed:{
        ...mapGetters(["UserInfo"]),
    },
    methods:{
        async RequestPost(op,postData=""){
            let data = await Request.post(this.$options.name, op, postData);
            return data;
        }
    },
    mounted(){
        let perms = store.getters.PathPermMap(this.$options.name);
        this.Perm  = new Proxy(this.Perm, {
            get(target, key) {
                key = key.replace(key[0],key[0].toLowerCase());
                if(!target.hasOwnProperty(key)){
                    target[key] =  perms.indexOf(key)>0;
                }
                return target[key];
            }
        });
        try{
            this.initView(true);
        }catch(e){
            throw "initView方法未定义"+e
        }
    },
    beforeCreate(){
        this.$options.name = this.$options._componentTag.replace(/_/g, '/')
    }
}